package cqrtplm.controller;

import cqrtplm.vo.query.AdvancedQueryConditionVO;
import cqrtplm.vo.query.AdvancedQueryConfig;
import cqrtplm.util.AdvancedQueryUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.ty.basic.response.ResponseResult;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "查询-高级查询")
@RequestMapping("/api/advancedQuery")
public class AdvancedQueryController {

    @Resource
    private AdvancedQueryUtil advancedQueryUtil;

    /**
     * 完全动态查询 - 客户端提交完整配置
     */
    @PostMapping("/simple")
    public ResponseResult simpleQuery(
            @RequestBody Map<String, Object> queryRequest) {
            // 从请求中提取配置和条件
            AdvancedQueryConfig config = extractConfigFromRequest(queryRequest);
            List<AdvancedQueryConditionVO> conditions = extractConditionsFromRequest(queryRequest);

            List<Map<String, Object>> queryReult = advancedQueryUtil.executeSecureQuery(config, conditions);
        return ResponseResult.success(queryReult);
    }

    /**
     * 多表联合查询
     */
    @PostMapping("/join")
    public ResponseResult<List<Map<String, Object>>> joinQuery(
            @RequestBody Map<String, Object> joinRequest) {
            AdvancedQueryConfig config = buildJoinConfig(joinRequest);
            List<AdvancedQueryConditionVO> conditions = extractConditionsFromRequest(joinRequest);

            List<Map<String, Object>> queryReult = advancedQueryUtil.executeSecureQuery(config, conditions);
            return ResponseResult.success(queryReult);
    }

    /**
     * 从请求中提取配置
     */
    private AdvancedQueryConfig extractConfigFromRequest(Map<String, Object> request) {
        String mainTable = (String) request.getOrDefault("mainTable", "DEFAULT_TABLE");
        AdvancedQueryConfig config = new AdvancedQueryConfig(mainTable);

        // 添加字段映射
        if (request.containsKey("fieldMappings")) {
            Map<String, String> mappings = (Map<String, String>) request.get("fieldMappings");
            mappings.forEach(config::addFieldMapping);
        }

        // 添加表别名
        if (request.containsKey("tableAliases")) {
            Map<String, String> aliases = (Map<String, String>) request.get("tableAliases");
            aliases.forEach(config::addTableAlias);
        }

        return config;
    }

    /**
     * 从请求中提取条件
     */
    private List<AdvancedQueryConditionVO> extractConditionsFromRequest(Map<String, Object> request) {
        // 这里需要根据实际请求结构解析条件
        // 示例实现，实际需要根据前端数据结构调整
        return (List<AdvancedQueryConditionVO>) request.get("conditions");
    }

    /**
     * 构建联合查询配置
     */
    private AdvancedQueryConfig buildJoinConfig(Map<String, Object> joinRequest) {
        // 这里实现多表联合查询的配置构建
        // 示例：简单的联合查询配置
        AdvancedQueryConfig config = new AdvancedQueryConfig("main_table");

        // 添加表别名
        config.addTableAlias("table1", "t1");
        config.addTableAlias("table2", "t2");

        // 添加字段映射
        config.addFieldMapping("id", "t1.id");
        config.addFieldMapping("name", "t1.name");
        config.addFieldMapping("type", "t2.type");

        return config;
    }

}